<?php

class HproseResultMode {
const Normal = 0;
const Serialized = 1;
const Raw = 2;
const RawWithEndTag = 3;
}
class HproseException extends Exception {}
class HproseDate {
public $year;
public $month;
public $day;
public $utc = false;
public function __construct() {
$IIIIIIIIIIIl = func_num_args();
$args = func_get_args();
switch ($IIIIIIIIIIIl) {
case 0:
$time = getdate();
$this->year = $time['year'];
$this->month = $time['mon'];
$this->day = $time['mday'];
break;
case 1:
$time = false;
if (is_int($args[0])) {
$time = getdate($args[0]);
}
elseif (is_string($args[0])) {
$time = getdate(strtotime($args[0]));
}
if (is_array($time)) {
$this->year = $time['year'];
$this->month = $time['mon'];
$this->day = $time['mday'];
}
elseif ($args[0] instanceof HproseDate) {
$this->year = $args[0]->year;
$this->month = $args[0]->month;
$this->day = $args[0]->day;
}
else {
throw new HproseException('Unexpected arguments');
}
break;
case 4:
$this->utc = $args[3];
case 3:
if (!self::isValidDate($args[0],$args[1],$args[2])) {
throw new HproseException('Unexpected arguments');
}
$this->year = $args[0];
$this->month = $args[1];
$this->day = $args[2];
break;
default:
throw new HproseException('Unexpected arguments');
}
}
public function addDays($days) {
if (!is_int($days)) return false;
$year = $this->year;
if ($days == 0) return true;
if ($days >= 146097 ||$days <= -146097) {
$IIIIIIIIII1l = $days %146097;
if ($IIIIIIIIII1l <0) {
$IIIIIIIIII1l += 146097;
}
$years = 400 * (int)(($days -$IIIIIIIIII1l) / 146097);
$year += $years;
if ($year <1 ||$year >9999) return false;
$days = $IIIIIIIIII1l;
}
if ($days >= 36524 ||$days <= -36524) {
$IIIIIIIIII1l = $days %36524;
if ($IIIIIIIIII1l <0) {
$IIIIIIIIII1l += 36524;
}
$years = 100 * (int)(($days -$IIIIIIIIII1l) / 36524);
$year += $years;
if ($year <1 ||$year >9999) return false;
$days = $IIIIIIIIII1l;
}
if ($days >= 1461 ||$days <= -1461) {
$IIIIIIIIII1l = $days %1461;
if ($IIIIIIIIII1l <0) {
$IIIIIIIIII1l += 1461;
}
$years = 4 * (int)(($days -$IIIIIIIIII1l) / 1461);
$year += $years;
if ($year <1 ||$year >9999) return false;
$days = $IIIIIIIIII1l;
}
$month = $this->month;
while ($days >= 365) {
if ($year >= 9999) return false;
if ($month <= 2) {
if ((($year %4) == 0) ?(($year %100) == 0) ?(($year %400) == 0) : true : false) {
$days -= 366;
}
else {
$days -= 365;
}
$year++;
}
else {
$year++;
if ((($year %4) == 0) ?(($year %100) == 0) ?(($year %400) == 0) : true : false) {
$days -= 366;
}
else {
$days -= 365;
}
}
}
while ($days <0) {
if ($year <= 1) return false;
if ($month <= 2) {
$year--;
if ((($year %4) == 0) ?(($year %100) == 0) ?(($year %400) == 0) : true : false) {
$days += 366;
}
else {
$days += 365;
}
}
else {
if ((($year %4) == 0) ?(($year %100) == 0) ?(($year %400) == 0) : true : false) {
$days += 366;
}
else {
$days += 365;
}
$year--;
}
}
$IIIIIIIIII11 = cal_days_in_month(CAL_GREGORIAN,$month,$year);
$day = $this->day;
while ($day +$days >$IIIIIIIIII11) {
$days -= $IIIIIIIIII11 -$day +1;
$month++;
if ($month >12) {
if ($year >= 9999) return false;
$year++;
$month = 1;
}
$day = 1;
$IIIIIIIIII11 = cal_days_in_month(CAL_GREGORIAN,$month,$year);
}
$day += $days;
$this->year = $year;
$this->month = $month;
$this->day = $day;
return true;
}
public function addMonths($months) {
if (!is_int($months)) return false;
if ($months == 0) return true;
$month = $this->month +$months;
$months = ($month -1) %12 +1;
if ($months <1) {
$months += 12;
}
$years = (int)(($month -$months) / 12);
if ($this->addYears($years)) {
$IIIIIIIIII11 = cal_days_in_month(CAL_GREGORIAN,$months,$this->year);
if ($this->day >$IIIIIIIIII11) {
$months++;
$this->day -= $IIIIIIIIII11;
}
$this->month = (int)$months;
return true;
}
else {
return false;
}
}
public function addYears($years) {
if (!is_int($years)) return false;
if ($years == 0) return true;
$year = $this->year +$years;
if ($year <1 ||$year >9999) return false;
$this->year = $year;
return true;
}
public function timestamp() {
if ($this->utc) {
return gmmktime(0,0,0,$this->month,$this->day,$this->year);
}
else {
return mktime(0,0,0,$this->month,$this->day,$this->year);
}
}
public function toString($fullformat = true) {
$IIIIIIIIIl1I = ($fullformat ?'%04d-%02d-%02d': '%04d%02d%02d');
$IIIIIIIIIl1l = sprintf($IIIIIIIIIl1I,$this->year,$this->month,$this->day);
if ($this->utc) {
$IIIIIIIIIl1l .= 'Z';
}
return $IIIIIIIIIl1l;
}
public function __toString() {
return $this->toString();
}
public static function isLeapYear($year) {
return (($year %4) == 0) ?(($year %100) == 0) ?(($year %400) == 0) : true : false;
}
public static function daysInMonth($year,$month) {
if (($month <1) ||($month >12)) {
return false;
}
return cal_days_in_month(CAL_GREGORIAN,$month,$year);
}
public static function isValidDate($year,$month,$day) {
if (($year >= 1) &&($year <= 9999)) {
return checkdate($month,$day,$year);
}
return false;
}
public function dayOfWeek() {
$IIIIIIIII11I = func_num_args();
if ($IIIIIIIII11I == 3) {
$args = func_get_args();
$IIIIIIIII11l = $args[0];
$IIIIIIIII111 = $args[1];
$IIIIIIIIlIII = $args[2];
}
else {
$IIIIIIIII11l = $this->year;
$IIIIIIIII111 = $this->month;
$IIIIIIIIlIII = $this->day;
}
$IIIIIIIIlIII += $IIIIIIIII111 <3 ?$IIIIIIIII11l--: $IIIIIIIII11l -2;
return ((int)(23 * $IIIIIIIII111 / 9) +$IIIIIIIIlIII +4 +(int)($IIIIIIIII11l / 4) -(int)($IIIIIIIII11l / 100) +(int)($IIIIIIIII11l / 400)) %7;
}
public function dayOfYear() {
static $IIIIIIIIlII1 = array(0,31,59,90,120,151,181,212,243,273,304,334,365);
static $IIIIIIIIlIlI = array(0,31,60,91,121,152,182,213,244,274,305,335,366);
$IIIIIIIII11I = func_num_args();
if ($IIIIIIIII11I == 3) {
$args = func_get_args();
$IIIIIIIII11l = $args[0];
$IIIIIIIII111 = $args[1];
$IIIIIIIIlIII = $args[2];
}
else {
$IIIIIIIII11l = $this->year;
$IIIIIIIII111 = $this->month;
$IIIIIIIIlIII = $this->day;
}
$days = self::isLeapYear($IIIIIIIII11l) ?$IIIIIIIIlII1 : $IIIIIIIIlIlI;
return $days[$IIIIIIIII111 -1] +$IIIIIIIIlIII;
}
}
class HproseTime {
public $hour;
public $minute;
public $second;
public $microsecond = 0;
public $utc = false;
public function __construct() {
$IIIIIIIIIIIl = func_num_args();
$args = func_get_args();
switch ($IIIIIIIIIIIl) {
case 0:
$time = getdate();
$IIIIIIIIlIll = gettimeofday();
$this->hour = $time['hours'];
$this->minute = $time['minutes'];
$this->second = $time['seconds'];
$this->microsecond = $IIIIIIIIlIll['usec'];
break;
case 1:
$time = false;
if (is_int($args[0])) {
$time = getdate($args[0]);
}
elseif (is_string($args[0])) {
$time = getdate(strtotime($args[0]));
}
if (is_array($time)) {
$this->hour = $time['hours'];
$this->minute = $time['minutes'];
$this->second = $time['seconds'];
}
elseif ($args[0] instanceof HproseTime) {
$this->hour = $args[0]->hour;
$this->minute = $args[0]->minute;
$this->second = $args[0]->second;
$this->microsecond = $args[0]->microsecond;
}
else {
throw new HproseException('Unexpected arguments');
}
break;
case 5:
$this->utc = $args[4];
case 4:
if (($args[3] <0) ||($args[3] >999999)) {
throw new HproseException('Unexpected arguments');
}
$this->microsecond = $args[3];
case 3:
if (!self::isValidTime($args[0],$args[1],$args[2])) {
throw new HproseException('Unexpected arguments');
}
$this->hour = $args[0];
$this->minute = $args[1];
$this->second = $args[2];
break;
default:
throw new HproseException('Unexpected arguments');
}
}
public function timestamp() {
if ($this->utc) {
return gmmktime($this->hour,$this->minute,$this->second) +
($this->microsecond / 1000000);
}
else {
return mktime($this->hour,$this->minute,$this->second) +
($this->microsecond / 1000000);
}
}
public function toString($fullformat = true) {
if ($this->microsecond == 0) {
$IIIIIIIIIl1I = ($fullformat ?'%02d:%02d:%02d': '%02d%02d%02d');
$IIIIIIIIIl1l = sprintf($IIIIIIIIIl1I,$this->hour,$this->minute,$this->second);
}
if ($this->microsecond %1000 == 0) {
$IIIIIIIIIl1I = ($fullformat ?'%02d:%02d:%02d.%03d': '%02d%02d%02d.%03d');
$IIIIIIIIIl1l = sprintf($IIIIIIIIIl1I,$this->hour,$this->minute,$this->second,(int)($this->microsecond / 1000));
}
else {
$IIIIIIIIIl1I = ($fullformat ?'%02d:%02d:%02d.%06d': '%02d%02d%02d.%06d');
$IIIIIIIIIl1l = sprintf($IIIIIIIIIl1I,$this->hour,$this->minute,$this->second,$this->microsecond);
}
if ($this->utc) {
$IIIIIIIIIl1l .= 'Z';
}
return $IIIIIIIIIl1l;
}
public function __toString() {
return $this->toString();
}
public static function isValidTime($hour,$minute,$second,$microsecond = 0) {
return !(($hour <0) ||($hour >23) ||
($minute <0) ||($minute >59) ||
($second <0) ||($second >59) ||
($microsecond <0) ||($microsecond >999999));
}
}
class HproseDateTime extends HproseDate {
public $hour;
public $minute;
public $second;
public $microsecond = 0;
public function __construct() {
$IIIIIIIIIIIl = func_num_args();
$args = func_get_args();
switch ($IIIIIIIIIIIl) {
case 0:
$time = getdate();
$IIIIIIIIlIll = gettimeofday();
$this->year = $time['year'];
$this->month = $time['mon'];
$this->day = $time['mday'];
$this->hour = $time['hours'];
$this->minute = $time['minutes'];
$this->second = $time['seconds'];
$this->microsecond = $IIIIIIIIlIll['usec'];
break;
case 1:
$time = false;
if (is_int($args[0])) {
$time = getdate($args[0]);
}
elseif (is_string($args[0])) {
$time = getdate(strtotime($args[0]));
}
if (is_array($time)) {
$this->year = $time['year'];
$this->month = $time['mon'];
$this->day = $time['mday'];
$this->hour = $time['hours'];
$this->minute = $time['minutes'];
$this->second = $time['seconds'];
}
elseif ($args[0] instanceof HproseDate) {
$this->year = $args[0]->year;
$this->month = $args[0]->month;
$this->day = $args[0]->day;
$this->hour = 0;
$this->minute = 0;
$this->second = 0;
}
elseif ($args[0] instanceof HproseTime) {
$this->year = 1970;
$this->month = 1;
$this->day = 1;
$this->hour = $args[0]->hour;
$this->minute = $args[0]->minute;
$this->second = $args[0]->second;
$this->microsecond = $args[0]->microsecond;
}
elseif ($args[0] instanceof HproseDateTime) {
$this->year = $args[0]->year;
$this->month = $args[0]->month;
$this->day = $args[0]->day;
$this->hour = $args[0]->hour;
$this->minute = $args[0]->minute;
$this->second = $args[0]->second;
$this->microsecond = $args[0]->microsecond;
}
else {
throw new HproseException('Unexpected arguments');
}
break;
case 2:
if (($args[0] instanceof HproseDate) &&($args[1] instanceof HproseTime)) {
$this->year = $args[0]->year;
$this->month = $args[0]->month;
$this->day = $args[0]->day;
$this->hour = $args[1]->hour;
$this->minute = $args[1]->minute;
$this->second = $args[1]->second;
$this->microsecond = $args[1]->microsecond;
}
else {
throw new HproseException('Unexpected arguments');
}
break;
case 3:
if (!self::isValidDate($args[0],$args[1],$args[2])) {
throw new HproseException('Unexpected arguments');
}
$this->year = $args[0];
$this->month = $args[1];
$this->day = $args[2];
$this->hour = 0;
$this->minute = 0;
$this->second = 0;
break;
case 8:
$this->utc = $args[7];
case 7:
if (($args[6] <0) ||($args[6] >999999)) {
throw new HproseException('Unexpected arguments');
}
$this->microsecond = $args[6];
case 6:
if (!self::isValidDate($args[0],$args[1],$args[2])) {
throw new HproseException('Unexpected arguments');
}
if (!self::isValidTime($args[3],$args[4],$args[5])) {
throw new HproseException('Unexpected arguments');
}
$this->year = $args[0];
$this->month = $args[1];
$this->day = $args[2];
$this->hour = $args[3];
$this->minute = $args[4];
$this->second = $args[5];
break;
default:
throw new HproseException('Unexpected arguments');
}
}
public function addMicroseconds($microseconds) {
if (!is_int($microseconds)) return false;
if ($microseconds == 0) return true;
$microsecond = $this->microsecond +$microseconds;
$microseconds = $microsecond %1000000;
if ($microseconds <0) {
$microseconds += 1000000;
}
$seconds = (int)(($microsecond -$microseconds) / 1000000);
if ($this->addSeconds($seconds)) {
$this->microsecond = (int)$microseconds;
return true;
}
else {
return false;
}
}
public function addSeconds($seconds) {
if (!is_int($seconds)) return false;
if ($seconds == 0) return true;
$second = $this->second +$seconds;
$seconds = $second %60;
if ($seconds <0) {
$seconds += 60;
}
$minutes = (int)(($second -$seconds) / 60);
if ($this->addMinutes($minutes)) {
$this->second = (int)$seconds;
return true;
}
else {
return false;
}
}
public function addMinutes($minutes) {
if (!is_int($minutes)) return false;
if ($minutes == 0) return true;
$minute = $this->minute +$minutes;
$minutes = $minute %60;
if ($minutes <0) {
$minutes += 60;
}
$hours = (int)(($minute -$minutes) / 60);
if ($this->addHours($hours)) {
$this->minute = (int)$minutes;
return true;
}
else {
return false;
}
}
public function addHours($hours) {
if (!is_int($hours)) return false;
if ($hours == 0) return true;
$hour = $this->hour +$hours;
$hours = $hour %24;
if ($hours <0) {
$hours += 24;
}
$days = (int)(($hour -$hours) / 24);
if ($this->addDays($days)) {
$this->hour = (int)$hours;
return true;
}
else {
return false;
}
}
public function after($when) {
if (!($when instanceof HproseDateTime)) {
$when = new HproseDateTime($when);
}
if ($this->utc != $when->utc) return ($this->timestamp() >$when->timestamp());
if ($this->year <$when->year) return false;
if ($this->year >$when->year) return true;
if ($this->month <$when->month) return false;
if ($this->month >$when->month) return true;
if ($this->day <$when->day) return false;
if ($this->day >$when->day) return true;
if ($this->hour <$when->hour) return false;
if ($this->hour >$when->hour) return true;
if ($this->minute <$when->minute) return false;
if ($this->minute >$when->minute) return true;
if ($this->second <$when->second) return false;
if ($this->second >$when->second) return true;
if ($this->microsecond <$when->microsecond) return false;
if ($this->microsecond >$when->microsecond) return true;
return false;
}
public function before($when) {
if (!($when instanceof HproseDateTime)) {
$when = new HproseDateTime($when);
}
if ($this->utc != $when->utc) return ($this->timestamp() <$when->timestamp());
if ($this->year <$when->year) return true;
if ($this->year >$when->year) return false;
if ($this->month <$when->month) return true;
if ($this->month >$when->month) return false;
if ($this->day <$when->day) return true;
if ($this->day >$when->day) return false;
if ($this->hour <$when->hour) return true;
if ($this->hour >$when->hour) return false;
if ($this->minute <$when->minute) return true;
if ($this->minute >$when->minute) return false;
if ($this->second <$when->second) return true;
if ($this->second >$when->second) return false;
if ($this->microsecond <$when->microsecond) return true;
if ($this->microsecond >$when->microsecond) return false;
return false;
}
public function equals($when) {
if (!($when instanceof HproseDateTime)) {
$when = new HproseDateTime($when);
}
if ($this->utc != $when->utc) return ($this->timestamp() == $when->timestamp());
return (($this->year == $when->year) &&
($this->month == $when->month) &&
($this->day == $when->day) &&
($this->hour == $when->hour) &&
($this->minute == $when->minute) &&
($this->second == $when->second) &&
($this->microsecond == $when->microsecond));
}
public function timestamp() {
if ($this->utc) {
return gmmktime($this->hour,
$this->minute,
$this->second,
$this->month,
$this->day,
$this->year) +
($this->microsecond / 1000000);
}
else {
return mktime($this->hour,
$this->minute,
$this->second,
$this->month,
$this->day,
$this->year) +
($this->microsecond / 1000000);
}
}
public function toString($fullformat = true) {
if ($this->microsecond == 0) {
$IIIIIIIIIl1I = ($fullformat ?'%04d-%02d-%02dT%02d:%02d:%02d'
: '%04d%02d%02dT%02d%02d%02d');
$IIIIIIIIIl1l = sprintf($IIIIIIIIIl1I,
$this->year,$this->month,$this->day,
$this->hour,$this->minute,$this->second);
}
if ($this->microsecond %1000 == 0) {
$IIIIIIIIIl1I = ($fullformat ?'%04d-%02d-%02dT%02d:%02d:%02d.%03d'
: '%04d%02d%02dT%02d%02d%02d.%03d');
$IIIIIIIIIl1l = sprintf($IIIIIIIIIl1I,
$this->year,$this->month,$this->day,
$this->hour,$this->minute,$this->second,
(int)($this->microsecond / 1000));
}
else {
$IIIIIIIIIl1I = ($fullformat ?'%04d-%02d-%02dT%02d:%02d:%02d.%06d'
: '%04d%02d%02dT%02d%02d%02d.%06d');
$IIIIIIIIIl1l = sprintf($IIIIIIIIIl1I,
$this->year,$this->month,$this->day,
$this->hour,$this->minute,$this->second,
$this->microsecond);
}
if ($this->utc) {
$IIIIIIIIIl1l .= 'Z';
}
return $IIIIIIIIIl1l;
}
public function __toString() {
return $this->toString();
}
public static function isValidTime($hour,$minute,$second,$microsecond = 0) {
return HproseTime::isValidTime($hour,$minute,$second,$microsecond);
}
}
function is_utf8($string) {
return preg_match('%^(?:[\x00-\x7F]|[\xC2-\xDF][\x80-\xBF]|\xE0[\xA0-\xBF][\x80-\xBF]|[\xE1-\xEC\xEE\xEF][\x80-\xBF]{2}|\xED[\x80-\x9F][\x80-\xBF]|\xF0[\x90-\xBF][\x80-\xBF]{2}|[\xF1-\xF3][\x80-\xBF]{3}|\xF4[\x80-\x8F][\x80-\xBF]{2})*$%xs',$string);
}
function ustrlen($s) {
$pos = 0;
$length = strlen($s);
$IIIIIIIIll11 = $length;
while ($pos <$length) {
$a = ord($s{$pos++});
if ($a <0x80) {
continue;
}
elseif (($a &0xE0) == 0xC0) {
++$pos;
--$IIIIIIIIll11;
}
elseif (($a &0xF0) == 0xE0) {
$pos += 2;
$IIIIIIIIll11 -= 2;
}
elseif (($a &0xF8) == 0xF0) {
$pos += 3;
$IIIIIIIIll11 -= 2;
}
}
return $IIIIIIIIll11;
}
function is_list($a) {
return is_array($a) &&((count($a) == 0) ||(count(array_diff(range(0,count($a) -1),array_keys($a))) == 0));
}
?>
